package telco.client;

import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import swa2012.telco.broker.beans.Client;
import swa2012.telco.broker.beans.Message;
import swa2012.telco.broker.beans.Provider;
import telco.provider.beans.ProviderRegistrationStatus;
import telco.provider.service.ClientService;

public class Model {

	ClientService provider;
	private Client me;
	private boolean CONNECTED = false;
	
	public Model() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(ClientService.class);
	    factory.setAddress("http://student.swa.univie.ac.at:8020/Provider/ClientService");
	    provider = (ClientService) factory.create();
	}

	public Client getMe() {
		return me;
	}
	
	public void setMe(String cId)
	{
		me = new Client(cId, new Provider("Group20", URI.create("http://student.swa.univie.ac.at:8020/Provider/BrokerCallback")));
	}
	
	public boolean getConnected(){
		return CONNECTED;
	}

	public String registerAtProvider() {
		ProviderRegistrationStatus res = provider.registerClient(me);
		
		if(res == ProviderRegistrationStatus.REGISTRATION_OK)
		{
			CONNECTED = true;
			return "Registered successful!";
		}
		else
		{
			return "Client with this ID allready registered!\nPlease enter a different ClientID and try again...";
		}
	}

	public String deregisterAtProvider() {
		ProviderRegistrationStatus res = provider.deregisterClient(me);
		CONNECTED = false;
		
		if(res == ProviderRegistrationStatus.REGISTRATION_OK)
		{
			return "Deregistered successful!";
		}
		else
		{
			return "Client allready deregistered!";
		}
	}

	public Vector<String> getAvailableClients() {
		Vector<Client> clients = provider.getAvailableClients();
		Vector<String> clientIDs = new Vector<String>();
		
		clients.remove(me);
		
		if(clients != null)
		{			
			for(Client c:clients)
			{
				if(c.getID() != null && c.getProvider() != null)
					clientIDs.add(c.getID() + "@" + c.getProvider().getID());
			}
			
			return clientIDs;
		}
		
		else return null;
	}
	
	public Vector<String> getMessages(){
		Vector<String> msgText = new Vector<String>();
		Vector<Message> msg = provider.getMessagesForClient(me);
		
		DateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		
		if(msg != null){
			for(Message s:msg)
			{
				calendar.setTimeInMillis(s.getTimestamp());
				msgText.add(s.getSender().getID() + "@" + s.getSender().getProvider().getID() +
						" wrote (Time: " + f.format(calendar.getTime()) + "):\n          " + s.getText());
			}
			
			return msgText;
		}
		else return null;
	}

	public void sendMessage(String msgText, String selected) {
		/*
		 * recv[0] = ClientID
		 * recv[1] = ProviderID
		 */
		String[] recv = selected.split("@", 2);
		Provider recvProv;
		
		if(me.getProvider().getID().compareTo(recv[1]) == 0)
			recvProv = me.getProvider();
		else recvProv = getProviderFromId(recv[1]);
		
		Client recvClient = new Client(recv[0], recvProv);
		
		provider.receiveMessage(new Message(me, recvClient, msgText, System.currentTimeMillis()));
	}
	
	public Provider getProviderFromId(String id)
	{
		Vector<Provider> providers = provider.getAvailableProviders();
		
		for(Provider p:providers)
		{
			if(p.getID().compareTo(id) == 0)
				return p;
		}
		
		return null;
	}
}
